package com.myspace.activity;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.myspace.sdk.MSRequest;
import com.myspace.sdk.MSSDK;
import com.myspace.sdk.MSRequest.MSRequestCallback;

public class MSPhotoActivity extends MSBaseActivity {

	private static final String TAG = "MSPhotoActivity";
	private static final int IMAGE_DIALOG = 1;
	private static final int PROGRESS_DIALOG = 2;
	private static final int ACTIVITY_RESULT_IMAGE_SELECTED = RESULT_FIRST_USER;
	
	private Button mUploadButton;
	private Button mSelectButton;
	private ImageView mImageView;
	private Uri mImageUri;
	private ProgressDialog mProgressDialog;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setPageName(R.string.PAGE_TITLE_PHOTO);
		super.setMainView(R.layout.photo);
		
		mSelectButton = (Button) findViewById(R.id.selectPhotoButton);
		mSelectButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showImageSourceDialog();
			}
		});
	        
        mUploadButton = (Button) findViewById(R.id.uploadPhotoButton);
        mUploadButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				uploadPhoto();
			}
		});
        		
        mImageView = (ImageView) findViewById(R.id.photoImageView);  
    }
	
	private void uploadPhoto() {
		try {
			if(mImageUri != null) {
				showDialog(PROGRESS_DIALOG);
				
				Map<String, String> queryParams = new HashMap<String, String>();
				queryParams.put("caption", "MySpace Sample Image");

				MSSDK.uploadPhoto(this, queryParams, mImageUri, new UploadPhoto_MSRequestCallback());
			}
			else {
				Log.d(TAG, "Image uri is null");
				showImageSourceDialog();
			}
		} catch (Exception e) {
			Log.d(TAG, e.toString());
		}
	}
	
	public static boolean isSDCardPresent() {
	    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
	}
	
	private void showImageSourceDialog() {
		showDialog(IMAGE_DIALOG);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case IMAGE_DIALOG:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Choose Image Source")
				   .setPositiveButton("Gallery", imageSourcePositiveListener )
				   .setNeutralButton("Camera", imageSourceNeutralListener )
				   .setNegativeButton("Cancel", imageSourceNegativeListener );
			return builder.create();
		case PROGRESS_DIALOG:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setMessage("Please wait while uploading...");
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.setCancelable(true);
			return mProgressDialog;
		}
		return null;
	}

	DialogInterface.OnClickListener imageSourcePositiveListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			showNativePhotoGallery();
		}
	};

	DialogInterface.OnClickListener imageSourceNeutralListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		}
	};

	DialogInterface.OnClickListener imageSourceNegativeListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			finish();
		}
	};

	private void showNativePhotoGallery() {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		startActivityForResult(intent, ACTIVITY_RESULT_IMAGE_SELECTED);			
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		
		if (requestCode == ACTIVITY_RESULT_IMAGE_SELECTED) {
			if (resultCode == RESULT_OK) {
				Uri uri = intent.getData();
				try {
					mImageView.setImageURI(uri);
					mImageUri = uri;
				} catch(Exception e) {
					Log.d(TAG, e.toString());
				}
			}
		}
	}
	
	private class UploadPhoto_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			dismissDialog(PROGRESS_DIALOG);
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			dismissDialog(PROGRESS_DIALOG);
			mImageView.setImageURI(null);
			mImageUri = null;
			showToast("Upload Successful");
		}
	}
}
